home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Testing & Debugging / Debuggers & dcmds / MacsBug 6.5d12 / dcmds / C Samples / File.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  6.9 KB  |  309 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        File.c
  3.  
  4.     Contains:    This is the FCB dcmd.
  5.  
  6.     Written by:    JM3 = Jim Murphy
  7.                 DAL = Dave Lyons
  8.                 sad = Scott Douglas
  9.  
  10.     Copyright:    © 1988,1993-1995 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.          <6>     2/21/95    DAL        Made "file 0" show all open files except for fonts (to help
  15.                                     shrink Standard Logs). Bumped version to 3.0.1.
  16.          <5>   10-Dec-94    JM3        Updated for new format 3 dcmd requirements.
  17.          <4>     5/13/94    DAL        Deal with carriage returns inside filenames (changed put.c).
  18.          <3>   24-Jan-94    JM3        With the Universal Interfaces, EqualString is now in
  19.                                     TextUtils.h. Blah.
  20.          <2>     9/13/93    DAL        made some columns wider and fixed the filename-prefix feature to
  21.                                     work again
  22.  
  23.     Modification history:
  24.         29Nov88 sad        revised for new dcmd names.
  25.          5Oct88    sad        broke out formatting routines to put.c
  26.         30Sep88 sad        written
  27.  
  28.     The following MPW commands will build the dcmd and copy it to the
  29.     "Debugger Prefs" file in the System folder. The dcmd's name in
  30.     MacsBug will be the name of the file built by the Linker.
  31.     You must first copy dcmd.h, dcmdGlue.a.o and DRunTime.o from the
  32.     C Samples folder into this folder.
  33.  
  34.     C Put.c
  35.     C File.c
  36.     Link dcmdGlue.a.o File.c.o put.c.o DRuntime.o "{Libraries}"Interface.o -o File
  37.     BuildDcmd File 1003
  38.     Echo 'include "File";'    |    Rez -a -o "{systemFolder}Debugger Prefs"
  39. */
  40.  
  41. #include <Types.h>
  42. #include <Memory.h>
  43. #include <Files.h>
  44. #include <OSUtils.h>
  45. #include <TextUtils.h>
  46.  
  47. #include "dcmd.h"
  48. #include "put.h"
  49.  
  50. #define FSFCBLen 0x3f6
  51. #define FCBsPtr  0x34e
  52.  
  53.  
  54. typedef struct FCB
  55. {
  56.     unsigned long fcbFlNum;
  57.     unsigned char fcbMdRByt;
  58.     unsigned char fcbTypByt;
  59.     unsigned short fcbSBlk;
  60.     unsigned long fcbEOF;
  61.     unsigned long fcbPLen;
  62.     unsigned long fcbCrPs;
  63.     VCB* fcbVPtr;
  64.     void* fcbBfAdr;
  65.     unsigned short fcbFlPos;
  66.     unsigned long fcbClmpSize;
  67.     void* fcbBTCBPtr;
  68.     unsigned long fcbExtRec[3];
  69.     OSType fcbFType;
  70.     unsigned long fcbCatPos;
  71.     unsigned long fcbDirID;
  72.     char fcbCName[32];
  73. }
  74.     FCB;
  75.  
  76.  
  77. static void DrawHdr(void)
  78. {
  79. //                             1         2         3         4         5         6         7
  80. //                    1234567890123456789012345678901234567890123456789012345678901234567890
  81.     dcmdDrawLine("\pfRef File                   Vol         Type Fl Fork     LEof     Mark  FlNum Parent FCB at");
  82. }
  83.  
  84.  
  85. static Boolean IsFontFile(FCB *fcbp)
  86. {
  87.     OSType filetype = fcbp->fcbFType;
  88.  
  89.     if (filetype == 'FFIL' || filetype == 'ffil' || filetype == 'tfil')
  90.         return true;
  91.  
  92.     return false;
  93. }
  94.  
  95.  
  96. static void DrawFCB(int fref, FCB* fcbp)
  97. {
  98.     PutUHexWord(fref);
  99.     PutSpace();
  100.     PutPStrTruncTo(fcbp->fcbCName, 17+10);
  101.     PutSpace();
  102.     PutPStrTruncTo(fcbp->fcbVPtr->vcbVN, 26+10+3);
  103.     PutSpace();
  104.     PutOSType(fcbp->fcbFType);
  105.     PutSpace();
  106.     PutChar((fcbp->fcbMdRByt & 0x80) ? 'D' : 'd');
  107.     PutChar((fcbp->fcbMdRByt & 0x01) ? 'W' : 'w');
  108.     PutSpace();
  109.     if (fcbp->fcbMdRByt & 0x02)
  110.         PutPStr("\prsrc ");
  111.     else
  112.         PutPStr("\pdata ");
  113.     PutUDecTo(fcbp->fcbEOF,47+11+3);
  114.     PutSpace();
  115.     PutUDecTo(fcbp->fcbCrPs,55+12+3);
  116.     PutSpace();
  117.     PutUHexZTo(fcbp->fcbFlNum,6,62+12+3);
  118.     PutSpace();
  119.     PutUHexZTo(fcbp->fcbDirID,6,69+12+3);
  120.     PutSpace();
  121.     PutUHexZTo((unsigned long)fcbp,6,76+12+3);
  122.     PutLine();
  123. }
  124.  
  125.  
  126. // PrefixPStr
  127. //         returns true if astr is equal to a prefix of bstr.
  128. //        (astr must not be longer than 31 characters)
  129.  
  130. static Boolean PrefixPStr(const Str255 astr, const Str255 bstr)
  131. {
  132.     char newstr[31];
  133.     int alen = *astr;
  134.     int blen = *bstr;
  135.  
  136.     if (alen <= blen)
  137.     {
  138.         BlockMoveData(bstr+1, newstr+1, alen);
  139.         newstr[0] = alen;
  140.         return EqualString(astr, newstr, false, true);
  141.     }
  142.     else
  143.         return false;    
  144. }
  145.  
  146.  
  147.  
  148. pascal void CommandEntry(dcmdBlock* paramPtr)
  149. {
  150.     static const char usageStr[] = "\p[fRefNum|\"file name\"]";
  151.  
  152.     switch (paramPtr->request)
  153.     {
  154.         case dcmdInit:
  155.             break;
  156.  
  157.         case dcmdHelp:
  158.             dcmdDrawLine("\pDisplays file information on all open files, or for the given fRefNum");
  159.             dcmdDrawLine("\por filename. Flags are D/d=Dirty, W/w=writeable.  \"file 0\" shows all");
  160.             dcmdDrawLine("\popen files except for fonts.");
  161.             break;
  162.  
  163.         case dcmdGetInfo:
  164.             * (long *) &((GetInfoRequestBlockPtr) paramPtr->requestIOBlock)->dcmdVersion = 0x03018000; // version 3.0.1 final
  165.             BlockMoveData(&usageStr, &((GetInfoRequestBlockPtr) paramPtr->requestIOBlock)->usageStr, usageStr[0]+1);
  166.             break;
  167.  
  168.         case dcmdDoIt:
  169.         {
  170.             Boolean    doOneFCB = false;
  171.             long    fref;
  172.             short    c;
  173.             Boolean    haveFileName = false;
  174.             Str255    filename;
  175.             int        fcbLen;                // the length of one fcb
  176.             char*    fcbsbase;
  177.             int        fcbslen;            // the length of the fcbs block
  178.  
  179.             dcmdSwapWorlds();
  180.  
  181.             dcmdDrawLine("\pDisplaying File Control Blocks");
  182.  
  183.             // get low-memory values after dcmdSwapWorlds()
  184.             fcbLen   = * (unsigned short *) FSFCBLen;
  185.             fcbsbase = * (char **) FCBsPtr;
  186.             fcbslen  = * (unsigned short *) fcbsbase;
  187.  
  188.             if (fcbLen != sizeof(FCB))
  189.             {
  190.                 PutPStr("\FSFCBLen = ");
  191.                 PutUDec(fcbLen);
  192.                 PutPStr("\p expected ");
  193.                 PutUDec(fcbLen);
  194.                 PutLine(sizeof(FCB));
  195.             }
  196.             if ((fcbslen - 2) % fcbLen != 0)
  197.             {
  198.                 PutPStr("\pBad fcbslen ");
  199.                 PutUHexWord(fcbslen);
  200.                 PutLine();
  201.             }
  202.  
  203.             c = dcmdPeekAtNextChar();
  204.             if (c == '"' || c == '\'')
  205.             {
  206.                 haveFileName = true;
  207.                 (void) dcmdGetNextParameter(filename);
  208.             }
  209.             else
  210.                 (void) dcmdGetNextExpression(&fref, &doOneFCB);
  211.  
  212.             if (doOneFCB && fref)
  213.             {
  214.                 fref = (unsigned short) fref;
  215.                 if ((fref > fcbslen) || ((fref - 2) % fcbLen != 0))
  216.                 {
  217.                     PutPStr("\pBad file refnum ");
  218.                     PutUHexWord(fref);
  219.                     PutLine();
  220.                 }
  221.                 else
  222.                 {
  223.                     FCB* fcbp = (FCB*)(fref + fcbsbase);
  224.                     if (fcbp->fcbFlNum)
  225.                     {
  226.                         DrawHdr();
  227.                         DrawFCB(fref,fcbp);
  228.                     }
  229.                     else
  230.                     {
  231.                         PutPStr("\pFCB ");
  232.                         PutUHexWord(fref);
  233.                         PutPStr("\p is not in use");
  234.                         PutLine();
  235.                     }
  236.                 }
  237.             }
  238.             else        // there was a refnum on the command line
  239.             {
  240.                 int numFCBs = (fcbslen - 2) / fcbLen;
  241.                 int fcbsUsed = 0;
  242.                 int numFontsSkipped = 0;
  243.                 Boolean foundOne = false;
  244.  
  245.                 for (fref = 2; (fref < fcbslen) && (!paramPtr->aborted); fref += fcbLen)
  246.                 {
  247.                     FCB* fcbp = (FCB *) (fcbsbase + fref);
  248.  
  249.                     if (fcbp->fcbFlNum && (!haveFileName || PrefixPStr(filename,fcbp->fcbCName)))
  250.                     {
  251.                         fcbsUsed++;
  252.                         if (!foundOne)
  253.                         {
  254.                             DrawHdr();
  255.                             foundOne = true;
  256.                         }
  257.  
  258.                         if (doOneFCB && IsFontFile(fcbp))
  259.                             numFontsSkipped++;
  260.                         else
  261.                             DrawFCB(fref,fcbp);
  262.                     }
  263.                 }
  264.  
  265.                 if (!paramPtr->aborted)
  266.                 {
  267.                     if (haveFileName)
  268.                     {
  269.                         if (!foundOne)
  270.                         {
  271.                             PutPStr("\pNo open files match \"");
  272.                             PutPStr(filename);
  273.                             PutChar('"');
  274.                             PutLine();
  275.                         }
  276.                     }
  277.                     else
  278.                     {
  279.                         PutUDec(numFCBs);
  280.                         PutPStr("\p FCBs, ");
  281.                         PutUDec(fcbsUsed);
  282.                         PutPStr("\p in use");
  283.  
  284.                         if (numFontsSkipped)
  285.                         {
  286.                             PutPStr("\p (including ");
  287.                             PutUDec(numFontsSkipped);
  288.                             PutPStr("\p fonts not listed)");
  289.                         }
  290.                         PutPStr("\p, ");
  291.  
  292.                         PutUDec(numFCBs - fcbsUsed);
  293.                         PutPStr("\p free");
  294.                         PutLine();
  295.                     }
  296.                 }
  297.             }    
  298.  
  299.             dcmdSwapWorlds();
  300.             break;
  301.         }
  302.  
  303.         // Format 3 and newer dcmds must quietly ignore requests we don't recognize.
  304.  
  305.         default:
  306.             break;
  307.     }
  308. }
  309.